package com.newworld.dmsapp2.fragment.adapter;

/**
 * 任务列表Adapter
 * @author zuo
 *
 */
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.newworld.dmsapp2.R;
import com.newworld.dmsapp2.bean.Task;
import com.newworld.dmsapp2.util.Config;

/**
 * @author zuo 列表界面的adapter（维修进度、维修任务、派单、改派、回访）
 * 
 */
public class TodoTaskListAdapter extends BaseAdapter {

	private List<Task> tasklist;
	private LayoutInflater inflater;
	Context context;

	public TodoTaskListAdapter(Context context, List<Task> tasklist) {
		inflater = LayoutInflater.from(context);
		this.tasklist = tasklist;
		this.context = context;
	}

	@Override
	public int getCount() {
		return tasklist.size();
	}

	@Override
	public Object getItem(int position) {
		return tasklist.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint({ "ResourceAsColor", "NewApi" })
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(
					R.layout.activity_to_do_task_list_item, parent, false);
			holder = new ViewHolder();

			holder.housetx = (TextView) convertView.findViewById(R.id.house);
			holder.firststrtx = (TextView) convertView
					.findViewById(R.id.tv_submit_time_string);
			holder.firsttimetx = (TextView) convertView
					.findViewById(R.id.tv_submit_time);
			holder.state = (TextView) convertView.findViewById(R.id.state);
			holder.secondstrtx = (TextView) convertView
					.findViewById(R.id.tv_expect_time_string);
			holder.secondtimetx = (TextView) convertView
					.findViewById(R.id.tv_expect_time);
			holder.reddotImg = (ImageView) convertView
					.findViewById(R.id.iv_todotask_item_reddot);
			holder.housetx.setTag(position);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
			holder.housetx.setTag(position);
		}
		/*if (tasklist.get(position).getIschange() == 2) {
			holder.reddotImg.setVisibility(View.INVISIBLE);
		} else {
			holder.reddotImg.setVisibility(View.VISIBLE);
		}*/
		holder.reddotImg.setVisibility(View.INVISIBLE);
		if (tasklist.get(position).getTaskState().equals(Config.TASKSTATE_BEASKED)) {
			holder.housetx.setTextColor(context.getResources().getColor(R.color.nw_tx_orange));
		}
		holder.housetx.setText(tasklist.get(position).getHouseAllName());
		holder.firststrtx.setText(tasklist.get(position).getFirstTimeStr());
		holder.firsttimetx.setText(tasklist.get(position).getFirstTime());
		holder.state.setText(tasklist.get(position).getTaskState());
		holder.secondstrtx.setText(tasklist.get(position).getSecondTimeStr());
		holder.secondtimetx.setText(tasklist.get(position).getSecondTime());
		holder.housetx.setOnClickListener(new OnClickListener() {
		    Boolean flag = true;
		   @Override
		   public void onClick(View v) {
		    if(flag){
		    
		     flag = false;
		     holder.housetx.setEllipsize(null); // 展开
		     //tv.setSingleLine(flag);
		    }else{
		     flag = true;
		     holder.housetx.setEllipsize(TextUtils.TruncateAt.END); // 收缩
		     //tv.setSingleLine(flag);
		    }
		   }
		  });
		return convertView;
	}

	private class ViewHolder {

		TextView housetx;
		TextView firststrtx;
		TextView firsttimetx;
		TextView state;
		TextView secondstrtx;
		TextView secondtimetx;
		ImageView reddotImg;

	}
}
